﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace _5E.Utils.MathHelper.Extension
{
    /// <summary>
    /// 程序说明：算法扩展类
    /// 创建作者：ahl
    /// 创建时间：2012-03-25
    /// </summary>
    public static class MathExtension
    {
        #region 百分比

        /// <summary>
        /// 减少百分比，四舍五入
        /// </summary>
        /// <param name="value">数值</param>
        /// <param name="nowPercent">当前比例</param>
        /// <param name="maxPercent">最大比例</param>
        /// <returns></returns>
        public static Int32 ReducePercent(this Int32 value, Int32 nowRate, Int32 maxRate)
        {
            Int32[] values = GetPercentResult(value, nowRate, maxRate);
            return values[0] - values[1];
        }

        /// <summary>
        /// 增加百分比,四舍五入
        /// </summary>
        /// <param name="value">数值</param>
        /// <param name="nowRate">当前比例</param>
        /// <param name="maxRate">最大比例</param>
        /// <returns></returns>
        public static Int32 IncreasePercent(this Int32 value, Int32 nowRate, Int32 maxRate)
        {
            Int32[] values = GetPercentResult(value, nowRate, maxRate);
            return values[0] + values[1];
        }

        /// <summary>
        /// 获取百分比计算值
        /// </summary>
        /// <param name="value">数值</param>
        /// <param name="nowRate">当前比例</param>
        /// <param name="maxRate">最大比例</param>
        /// <returns></returns>
        private static Int32[] GetPercentResult(Int32 value, Int32 nowRate, Int32 maxRate)
        {
            Int32[] result = new Int32[2];
            result[0] = value;
            Int32 temp = result[0] * nowRate;
            result[1] = temp / maxRate;
            result[1] += Convert.ToDouble(temp % maxRate) / maxRate >= 0.5 ? 1 : 0;
            return result;
        }

        #endregion

        #region 概率

        /// <summary>
        /// 触发概率
        /// </summary>
        /// <param name="now">当前概率</param>
        /// <param name="max">最大概率</param>
        /// <returns></returns>
        public static bool TriggleProbability(this Int32 now, Int32 max)
        {
            Probability prob = new Probability(now, max);
            return prob.IsTrigger;
        }


        #endregion

        #region 随机抽数

        #endregion
    }
}
